package com.check.checksamejourney.mapper;

import com.check.checksamejourney.bean.Journey;
import com.check.checksamejourney.bean.Search;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface JourneyMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into journey(o_id,date,start_time,end_time,type,site,remark,depart_pos,arrive_pos,source,link,create_time,update_time,is_deleted) " +
            "values(#{oId},#{date},#{startTime},#{endTime},#{type},#{site},#{remark},#{departPos},#{arrivePos},#{source},#{link},now(),now(),0)")
    int insertJourney(Journey journey);

    @Delete("delete from journey where id = #{id}")
    int deleteJourneyById(Integer id);

    @Select("select * from journey where is_deleted = 0 and id = #{id}")
    Journey getJourneyById(Long id);

    @SelectProvider(type = JourneyMapperProvider.class, method = "getJourneys")
    List<Journey> getJourneys(Search search);

    class JourneyMapperProvider{
        public String getJourneys(Search search){
            String sql = "select * from journey where is_deleted = 0 ";

            String addition = "and ";
            if (search.getDate()!=null && !"".equals(search.getDate())){
                sql += addition + "date = #{date} ";
//                addition = "and ";
            }

            if (search.getSite()!=null && !"".equals(search.getSite())){
                sql +=  addition + "(site like concat('%',#{site},'%') " +
                        "or source like concat('%',#{site},'%') " +
                        "or depart_pos like concat('%',#{site},'%') " +
                        "or arrive_pos like concat('%',#{site},'%')) ";
//                addition = "and ";
            }
/*            if (search.getRegion()!=null && !"".equals(search.getRegion())){
                sql +=  addition + "(site like concat('%',#{region},'%') " +
                        "or source like concat('%',#{region},'%') " +
                        "or depart_pos like concat('%',#{region},'%') " +
                        "or arrive_pos like concat('%',#{region},'%') )";
                addition = "and ";
            }*/
            if (search.getType()!=null && 0!=search.getType()){
                sql += addition + "type = #{type} ";
            }

            if (search.getLimit()==null){
                sql += "limit 20 ";
            }else {
                sql += "limit #{limit} ";
            }
            if (search.getOffset()!=null){
                sql += "offset #{offset} ";
            }

            return sql;
        }
    }

}
